<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Alternating Iterations</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Alternating Iterations</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><link href="../assets/anim/anim.css" rel="stylesheet" type="text/css">
<div class="intro">
    <p>This demonstrates how to use the <code>iterations</code> attribute to run multiple iterations of the animation.</p>
    <p>Mouse over the link to begin the animation.</p>
</div>

<div class="example">
<a href="#" id="demo">hover me</a>

<script type="text/javascript">
YUI().use('anim', function(Y) {
    var node = Y.one('#demo');

    var anim = new Y.Anim({
        node: node,
        from: {
            backgroundColor:node.getStyle('backgroundColor'),
            color: node.getStyle('color'),
            borderColor: node.getStyle('borderTopColor')
        },

        to: {
            color: '#fff',
            backgroundColor:'#ffa928',
            borderColor: '#71241a'
        },

        duration: 0.5,
        iterations: 'infinite',
        direction: 'alternate'
    });

    var hover = function(e) {
        var reverse = false,
            iterations = 'infinite';

        if (anim.get('running')) {
            anim.stop();
        }

        if (e.type === 'mouseout') {
            reverse = true;
            iterations = 1;
        }

        anim.setAttrs({
            'reverse': reverse,
            'iterations': iterations
        });

        anim.run();
    };

    node.on('mouseover', hover);
    node.on('mouseout', hover);
});

</script>

</div>

<h2>Setting up the HTML</h2>
<p>First we add some HTML to animate.</p>

<pre class="code prettyprint">&lt;a href=&quot;#&quot; id=&quot;demo&quot;&gt;hover me&lt;&#x2F;a&gt;</pre>


<h2>Creating the Anim Instance</h2>
<p>Now we create an instance of <code>Y.Anim</code>, passing it a configuration object that includes the <code>node</code> we wish to animate and the <code>to</code> attribute containing the final properties and their values.</p>
<p>Adding an <code>iterations</code> attribute of "infinite" means that the animation will run continuously.</p>
<p>The <code>direction</code> attribute of "alternate" means that the animation reverses every other iteration.</p>

<pre class="code prettyprint">var node = Y.one(&#x27;#demo&#x27;);

var anim = new Y.Anim({
    node: node,
    from: {
        backgroundColor:node.getStyle(&#x27;backgroundColor&#x27;),
        color: node.getStyle(&#x27;color&#x27;),
        borderColor: node.getStyle(&#x27;borderTopColor&#x27;)
    },

    to: {
        color: &#x27;#fff&#x27;,
        backgroundColor:&#x27;#ffa928&#x27;,
        borderColor: &#x27;#71241a&#x27;
    },

    duration: 0.5,
    iterations: &#x27;infinite&#x27;,
    direction: &#x27;alternate&#x27;
});</pre>


<h2>Changing Attributes</h2>
<p>We don't want the animation running when the link is not hovered over, so we will change the animation attributes depending on whether or not we are over the link.</p>
<p>We can use a single handler for both mouse the mouseOver and mouseOut events, and set the <code>reverse</code> attribute depending on which event fired.</p>

<pre class="code prettyprint">var hover = function(e) {
    var reverse = false,
        iterations = &#x27;infinite&#x27;;

    if (anim.get(&#x27;running&#x27;)) {
        anim.pause();
    }
    if (e.type === &#x27;mouseout&#x27;) {
        reverse = true;
        iterations = 1;
    }
    anim.setAttrs({
        &#x27;reverse&#x27;: reverse,
        &#x27;iterations&#x27;: iterations
    });

    anim.run();
};</pre>


<h2>Running the Animation</h2>
<p>Finally we add event handlers to run the animation.</p>

<pre class="code prettyprint">node.on(&#x27;mouseover&#x27;, hover);
node.on(&#x27;mouseout&#x27;, hover);</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;a href=&quot;#&quot; id=&quot;demo&quot;&gt;hover me&lt;&#x2F;a&gt;

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&#x27;anim&#x27;, function(Y) {
    var node = Y.one(&#x27;#demo&#x27;);

    var anim = new Y.Anim({
        node: node,
        from: {
            backgroundColor:node.getStyle(&#x27;backgroundColor&#x27;),
            color: node.getStyle(&#x27;color&#x27;),
            borderColor: node.getStyle(&#x27;borderTopColor&#x27;)
        },

        to: {
            color: &#x27;#fff&#x27;,
            backgroundColor:&#x27;#ffa928&#x27;,
            borderColor: &#x27;#71241a&#x27;
        },

        duration: 0.5,
        iterations: &#x27;infinite&#x27;,
        direction: &#x27;alternate&#x27;
    });

    var hover = function(e) {
        var reverse = false,
            iterations = &#x27;infinite&#x27;;

        if (anim.get(&#x27;running&#x27;)) {
            anim.stop();
        }

        if (e.type === &#x27;mouseout&#x27;) {
            reverse = true;
            iterations = 1;
        }

        anim.setAttrs({
            &#x27;reverse&#x27;: reverse,
            &#x27;iterations&#x27;: iterations
        });

        anim.run();
    };

    node.on(&#x27;mouseover&#x27;, hover);
    node.on(&#x27;mouseout&#x27;, hover);
});

&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Creating and using a simple animation.">
                                        <a href="basic.html">Basic Animation</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Animation Utility allows you to implement &#x27;easing effects&#x27; &mdash; for example, when an animation gradually slows down as it nears completion, that&#x27;s an easing effect known as &#x27;ease-in&#x27;.  This example shows you how to use easing effects with your animations.">
                                        <a href="easing.html">Easing Effects</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Color animations can be effective indicators of state during the lifespan of a dynamic page.  This example shows you how to animate color attributes of an element.">
                                        <a href="colors.html">Animating Colors</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The direction attribute can be used to reverse the animation on alternate iterations.">
                                        <a href="alt-iterations.html">Alternating Iterations</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows you how to animate the xy coordinates of an element.">
                                        <a href="anim-xy.html">Animating XY Position</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example demonstrates animating an element along a curved path using bezier control points.">
                                        <a href="curve.html">Animating Along a Curved Path</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The reverse attribute allows you to change the run direction of an animation.">
                                        <a href="reverse.html">Reversing an Animation</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example demonstrates how to use the end event.">
                                        <a href="end-event.html">Using the End Event</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to animate the Overlay&#x27;s movement and visibility.">
                                        <a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Working with multiple YUI instances.">
                                        <a href="../yui/yui-multi.html">Multiple Instances</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to make an animated node a Drop target.">
                                        <a href="../dd/anim-drop.html">Animated Drop Targets</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
